/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Block;

import DAO.DepartmentDAO;
import DAO.ConnectDatabase;
import DAO.RoomDAO;
import Helper.DepartmentHelper;
import Helper.DepartmentHelper;
import Helper.RoomHelper;
import Model.RoomModel;
import common.Constant;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author KIENLT
 */
public class Rooms extends javax.swing.JPanel {

    ArrayList<RoomHelper> arrRoom;
    ArrayList<RoomHelper> arrRoomList;
    RoomHelper roomData;
    ResultSet rs = null;

    /**
     * Creates new form Rooms
     */
    public Rooms() {
        initComponents();
        loadComboboxAccountLevel();
        fillAllRoom();
        changeLanguage();
    }

    private void changeLanguage() {
        if(Constant.getLanguage() == 0){
            Constant.initBundle();
            getLanguage();
        }else{
            Constant.initBundle();
            getLanguage();
        }
    }
    
    private void getLanguage() {
        lblRoomID.setText(Constant.getBundle().getString("roomID"));
        lblRoomName.setText(Constant.getBundle().getString("roomName"));
        lblDep.setText(Constant.getBundle().getString("department"));
        lblStatus.setText(Constant.getBundle().getString("roomStatus"));
        btnUpdate.setText(Constant.getBundle().getString("update"));
        btnDelete.setText(Constant.getBundle().getString("delete"));
        btnAdd.setText(Constant.getBundle().getString("add"));
    }
    private void loadComboboxAccountLevel() {
        try {
            DefaultComboBoxModel cbxDep = new DefaultComboBoxModel();
            ResultSet rs = DepartmentDAO.fillAllDepartment(ConnectDatabase.ConnectionDatabase());
            while (rs.next()) {
                String strId = rs.getString("DepartmentId");
                String strName = rs.getString("DepartmentName");
                cbxDep.addElement(new DepartmentHelper(strId, strName));
            }
            cbxDepartment.setModel(cbxDep);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex1);
            }
            ex.printStackTrace();
        }
    }

    private void fillAllRoom() {
        try {
            arrRoom = RoomDAO.fillAll(ConnectDatabase.ConnectionDatabase());
            arrRoomList = new ArrayList<RoomHelper>();

            for (RoomHelper item : arrRoom) {
                arrRoomList.add(item);
            }

            tblRoom.setModel(new RoomModel(arrRoomList));

            if (arrRoomList.size() > 0) {
                tblRoom.getSelectionModel().setSelectionInterval(0, 0);
                fillOneRoom();
            }

            tblRoom.getSelectionModel().addListSelectionListener(
                    new ListSelectionListener() {

                        @Override
                        public void valueChanged(ListSelectionEvent e) {

                            boolean adjust = e.getValueIsAdjusting();
                            if (adjust) {
                                fillOneRoom();
                            }

                        }
                    });

            DefaultTableColumnModel model = (DefaultTableColumnModel) tblRoom.getColumnModel();
            TableColumn tblCol = model.getColumn(1);
            tblCol.setPreferredWidth(250);
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(Rooms.class.getName()).log(Level.SEVERE, null, ex1);
            }
            ex.printStackTrace();
        }

    }

    // Hiển thị thong tin department
    private void fillOneRoom() {
        try {
            roomData = new RoomHelper();
            int row = tblRoom.getSelectedRow();
            roomData.setDepartmentID(tblRoom.getValueAt(row, 0).toString());
            rs = RoomDAO.FillOne(ConnectDatabase.ConnectionDatabase(), roomData);
            if (rs.next()) {
                txtRoomID.setText(rs.getString("RoomId"));
                txtRoomName.setText(rs.getString("RoomName"));
                int st = rs.getString("Status").equals("1") ? 1 : 0;
                cbxStatus.setSelectedIndex(st);
                int dep = Integer.parseInt(rs.getString("DepartmentId"));
                int cbx = dep - 1;
                cbxDepartment.setSelectedIndex(cbx);
            }
        } catch (Exception ex) {
            try {
                ConnectDatabase.CloseDB(rs);
                ConnectDatabase.CloseDB(ConnectDatabase.ConnectionDatabase());
            } catch (Exception ex1) {
                Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblRoom = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        cbxDepartment = new javax.swing.JComboBox();
        lblDep = new javax.swing.JLabel();
        lblRoomID = new javax.swing.JLabel();
        txtRoomID = new javax.swing.JTextField();
        lblRoomName = new javax.swing.JLabel();
        txtRoomName = new javax.swing.JTextField();
        lblStatus = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        cbxStatus = new javax.swing.JComboBox();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Room", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        tblRoom.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblRoom);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Room Infomation", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        lblDep.setText("Department");

        lblRoomID.setText("RoomID");

        txtRoomID.setEnabled(false);

        lblRoomName.setText("Room Name");

        lblStatus.setText("Stautus");

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/add2.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/update.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/img_x16/delete2.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        cbxStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Còn Trống", "Đầy" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRoomName)
                            .addComponent(lblRoomID))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtRoomID, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRoomName, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblDep)
                            .addComponent(lblStatus))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbxDepartment, 0, 152, Short.MAX_VALUE)
                            .addComponent(cbxStatus, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(btnAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnUpdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtRoomID, txtRoomName});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRoomID)
                            .addComponent(txtRoomID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRoomName)
                            .addComponent(txtRoomName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbxDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDep))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblStatus)
                            .addComponent(cbxStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete)
                    .addComponent(btnAdd))
                .addContainerGap(313, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        try {
            // TODO add your handling code here:
            RoomHelper room = new RoomHelper();
            int row = tblRoom.getSelectedRow();
            room.setRoomID(tblRoom.getValueAt(row, 0).toString());
            room.setRoomName(txtRoomName.getText());
            room.setRoomStatus(String.valueOf(cbxStatus.getSelectedIndex()));
            int cbx= Integer.parseInt(String.valueOf(cbxDepartment.getSelectedIndex()));
            int dep=cbx+1;
            room.setDepartmentID(String.valueOf(dep));

            if (!RoomDAO.checkRoom(ConnectDatabase.ConnectionDatabase(), room)) {
                int query = RoomDAO.Insert(ConnectDatabase.ConnectionDatabase(), room);
                if (query > 0) {
                    JOptionPane.showMessageDialog(this, "Insert Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                    fillAllRoom();
                } else {
                    JOptionPane.showMessageDialog(this, "Insert Fail.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(this, "UserName is exists.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(Rooms.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
     try {
            // TODO add your handling code here:
            RoomHelper room = new RoomHelper();
            room.setRoomID(txtRoomID.getText());
            room.setRoomName(txtRoomName.getText());
            //acc.setGender(String.valueOf(cbxGender.getSelectedIndex()));
           room.setRoomStatus(String.valueOf(cbxStatus.getSelectedIndex()));
            int cbx= Integer.parseInt(String.valueOf(cbxDepartment.getSelectedIndex()));
            int dep=cbx+1;
            room.setDepartmentID(String.valueOf(dep));
            int query = RoomDAO.Update(ConnectDatabase.ConnectionDatabase(), room);
            if(query > 0){
                JOptionPane.showMessageDialog(this, "Update Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                fillAllRoom();
            }else{
                JOptionPane.showMessageDialog(this, "Update Fail.", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception ex) {
            Logger.getLogger(GroupDoctors.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        //      removeTableAccount();
        try {
            RoomHelper room = new RoomHelper();
            room.setDepartmentID(txtRoomID.getText());
            int option = JOptionPane.showConfirmDialog(this, "You are sure delete?", "Message", JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                int query = RoomDAO.Delete(ConnectDatabase.ConnectionDatabase(), room);

                if (query > 0) {
                    JOptionPane.showMessageDialog(this, "Delete Success.", "Message", JOptionPane.INFORMATION_MESSAGE);
                    fillAllRoom();
                } else {
                    JOptionPane.showMessageDialog(this, "Delete Fail.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void removeTableAccount() {
        /*       // TODO add your handling code here:
        int[] rows = tblDoctor.getSelectedRows();
        for(int i=rows.length-1; i>=0; i--){
        AccountModel accModel = (AccountModel)tblDoctor.getModel();
        if(String.valueOf(tblDoctor.getValueAt(rows[i], 0)).equals("true")){
        int option = JOptionPane.showConfirmDialog(this, "You are sure delete?", "Message", JOptionPane.YES_NO_OPTION);
        if(option == JOptionPane.YES_OPTION){
        deleteAccount();
        accModel.deleteRow(rows[i]);
        }
        }
        }*/
}//GEN-LAST:event_btnDeleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbxDepartment;
    private javax.swing.JComboBox cbxStatus;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDep;
    private javax.swing.JLabel lblRoomID;
    private javax.swing.JLabel lblRoomName;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JTable tblRoom;
    private javax.swing.JTextField txtRoomID;
    private javax.swing.JTextField txtRoomName;
    // End of variables declaration//GEN-END:variables
}
